home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / softwareupdate / system / amigados / handlers / example5b.c < prev    next >
C/C++ Source or Header  |  1996-10-10  |  6KB  |  229 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE)           Amiga C Club (ACC) */
  4. /* --------------------------           ------------------ */
  5. /*                                                         */
  6. /* Manual:  AmigaDOS                    Amiga C Club       */
  7. /* Chapter: Handlers                    Tulevagen 22       */
  8. /* File:    Example5B.c                 181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    93-03-16                                       */
  11. /* Version: 1.3                                            */
  12. /*                                                         */
  13. /*   Copyright 1993, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how you can use the Pipe handler */
  21. /* to copy (pipe) data to another program. This is the second */
  22. /* part of the example, program B. See example 5A for more    */
  23. /* information.                                               */
  24.  
  25.  
  26.  
  27. /* Include the dos library definitions: */
  28. #include <dos/dos.h>
  29.  
  30. /* Now we include the necessary function prototype files:         */
  31. #include <clib/dos_protos.h>       /* General dos functions...    */
  32. #include <clib/exec_protos.h>      /* System functions...         */
  33. #include <stdio.h>                 /* Std functions [printf()...] */
  34. #include <stdlib.h>                /* Std functions [exit()...]   */
  35. #include <string.h>                /* Std functions [strlen()...] */
  36.  
  37.  
  38.  
  39. /* The maximum number of characters that we can store in */
  40. /* our small buffer (including a NULL sign at the end):  */
  41. #define MAX_LENGTH 512
  42.  
  43.  
  44.  
  45. /* Set name and version number: */
  46. UBYTE *version = "$VER: AmigaDOS/Handlers/Example5B 1.3";
  47.  
  48.  
  49.  
  50. /* Declared our own function(s): */
  51.  
  52. /* Our main function: */
  53. int main( int argc, char *argv[] );
  54.  
  55. /* Writes text to an already open file: */
  56. /* (e.g. File, Console or Pipe handler) */
  57. int print_text
  58. (
  59.   BPTR file,
  60.   STRPTR text
  61. );
  62.  
  63. /* Collects text from an already open file:*/
  64. /* (e.g. File, Console or Pipe handler)    */
  65. int collect_text
  66. (
  67.   BPTR file,
  68.   STRPTR text
  69. );
  70.  
  71.  
  72.  
  73. /* Main function: */
  74.  
  75. int main( int argc, char *argv[] )
  76. {
  77.   /* Store the number of characters used here: */
  78.   int number;
  79.  
  80.   /* Create a buffers: */
  81.   UBYTE user_input[ MAX_LENGTH ];
  82.  
  83.   /* A "BCPL" pointer to our Console window: */
  84.   BPTR my_console;
  85.  
  86.   /* A "BCPL" pointer to our Pipe handler: */
  87.   BPTR my_pipe;
  88.   
  89.  
  90.  
  91.   /* Open a Console window: */
  92.   my_console = 
  93.    Open( "CON:0/100/640/100/Program B/CLOSE/WAIT", MODE_NEWFILE );
  94.   
  95.   /* Have we opened the Console successfully? */
  96.   if( my_console == NULL )
  97.   {
  98.     /* Inform the user: */
  99.     printf( "Error! Could not open the Console device!\n" );
  100.  
  101.     /* Exit with an error code: */
  102.     exit( 20 );
  103.   }
  104.  
  105.  
  106.  
  107.   /* Open the Pipe handler: (We call the pipe "UserData", and  */
  108.   /* we open the pipe handler as an old handler. Program A     */
  109.   /* opens the handler as a new handler. Note that one program */
  110.   /* must open the handler as new and the other program open   */
  111.   /* the handler as old! The order does not matter.)           */
  112.   my_pipe = 
  113.    Open( "PIPE:UserData", MODE_OLDFILE );
  114.  
  115.   /* Have we opened the Pipe handler successfully? */
  116.   if( my_pipe == NULL )
  117.   {
  118.     /* Inform the user: */
  119.     printf( "Error! Could not open the Pipe handler!\n" );
  120.  
  121.     /* Note that since we have set the flag "WAIT" the Console */
  122.     /* window will not be closed when we close the "file". The */
  123.     /* user has to close it by him/her self. It is therefore   */
  124.     /* best to give the user some instructions in the Console  */
  125.     /* window itself:                                          */
  126.     print_text( my_console, "Error! Could not open the Pipe handler!\n" );
  127.     print_text( my_console, "Please close this window.\n\n" );
  128.     
  129.     /* Close the Console window: */
  130.     Close( my_console );
  131.  
  132.     /* Exit with an error code: */
  133.     exit( 21 );
  134.   }
  135.  
  136.  
  137.  
  138.   /* Tell the user what will happen: */
  139.   print_text( my_console,
  140.     "Start Example7A and type in some text. The text you enter will\n" );
  141.   print_text( my_console,
  142.     "be copied (piped) to this window when you press Enter.\n" );
  143.   print_text( my_console,
  144.     "Note that you can not close this window until some text\n" );
  145.   print_text( my_console,
  146.     "has arrived at the pipe \"PIPE:UserData\" handler.\n\nPiped: " );
  147.  
  148.   /* Collect some text from the Pipe handler: */
  149.   number = collect_text( my_pipe, user_input );
  150.  
  151.   /* Send the data we collected to the Console window: */
  152.   print_text( my_console, user_input );
  153.  
  154.   /* All text has now been piped: */
  155.   print_text( my_console,
  156.     "\nNo oil, but the text was at least piped!\n" );
  157.   print_text( my_console,
  158.     "Please close the window.\n" );
  159.  
  160.  
  161.  
  162.  
  163.   /* Close the Pipe handler: */
  164.   Close( my_pipe );
  165.  
  166.   /* Close the consle window: */
  167.   Close( my_console );
  168.  
  169.   /* Note! Since we have opened a Console window with a close */
  170.   /* gadget and set the flag "WAIT" the Console window will   */
  171.   /* actually first be closed when the user clicks on the     */
  172.   /* close gadget or types "CTRL-\".                          */
  173.  
  174.  
  175.  
  176.   /* The End! */
  177.   exit( 0 );
  178. }
  179.  
  180.  
  181.  
  182. /* Writes text to an already opened file, and returns */
  183. /* the number of characters actualy written.          */
  184.  
  185. int print_text
  186. (
  187.   BPTR file,
  188.   STRPTR text
  189. )
  190. {
  191.   /* Store the number of characters (bytes) actualy written here: */
  192.   int characters_written;
  193.  
  194.  
  195.  
  196.   /* Write the text: */
  197.   characters_written = Write( file, text, strlen( text ) );
  198.  
  199.   /* Returns the number of characters actually written: */
  200.   return( TRUE );
  201. }
  202.  
  203.  
  204.  
  205. /* Collects text from an already opened file, and returns */
  206. /* the number of characters collected.                    */
  207.  
  208. int collect_text
  209. (
  210.   BPTR file,
  211.   STRPTR text
  212. )
  213. {
  214.   /* Store the number of characters (bytes) actualy read here: */
  215.   int characters_read;
  216.  
  217.  
  218.  
  219.   /* Collect some text: */
  220.   characters_read = Read( file, text, MAX_LENGTH - 1 );
  221.  
  222.   /* Put the NULL ('\0') sign at the end of the string: */
  223.   text[ characters_read ] = NULL;
  224.  
  225.   /* Returns the number of characters collected: */
  226.   return( characters_read );
  227. }
  228.  
  229.